5b380145eb5007099ea3c81af14cf57917fc598f
Gitcub / pages / [[...]].js
import hljs from 'highlight.js';
import { IconContext } from 'react-icons';
import { FcFile, FcFolder } from 'react-icons/fc';

async function getServerSideProps(context) {
  const res = await fetch('http://localhost:3000/api' + context.resolvedUrl);
  const data = await res.json();

  return { props: { data } };
}

function MidSection(props) {
  return (
    <div className="h-16 p-8">
      {props.children}
    </div>
  );
}

function MainContentContainer(props) {
  return (
    <div className="border border-slate-400 rounded-lg p-8">
      {props.children}
    </div>
  );
}

function BreadCrumbTrail(props) {
  const pathArray = props.pathArray;
  
  const recursion = (acc) => {
    if (acc.length === pathArray.length) {
      return acc;
    } else {
      const subArray = pathArray.slice(0, acc.length + 1);

      const breadCrumb = {
        name: pathArray[acc.length],
        href: '/' + subArray.join('/'),
      };

      const newAcc = acc.concat([breadCrumb]);
      return recursion(newAcc);
    }
  };

  const breadCrumbs = recursion([]);
  
  // remove commit
  breadCrumbs.splice(1, 1);

  return (
    <div>
      {breadCrumbs.map((breadCrumb) => (
        <el>/ <a href={breadCrumb.href}>{breadCrumb.name}</a> </el>
      ))}
    </div>
  );
}

function DirectoryListing(props) {
  const parentPath = props.resultPath.length === 1 ? [props.resultPath[0], 'master'] : props.resultPath;
  const parentURL = '/' + parentPath.join('/') + '/'
  
  return (
    <IconContext.Provider value={{ size: "2em" }}>
      <ul className="text-lg divide-y divide-slate-300">
        {props.resultContent.trees.map((item) => (
          <li className="h-12 flex items-center">
            <div className="pr-4">
              <FcFolder />
            </div>
            <a href={parentURL + item}>{item}</a>
          </li>
        ))}
        {props.resultContent.blobs.map((item) => (
          <li className="h-12 flex items-center">
            <div className="pr-4">
              <FcFile />
            </div>
            <a href={parentURL + item}>{item}</a>
          </li>
        ))}
      </ul>
    </IconContext.Provider>
  );
}

function LineNos(props) {
  const count = props.textBlob.split('\n').length;
  const lineNos = [...Array(count).keys()].map((x) => x + 1).join('\n');

  return (
    <pre className="text-right text-slate-400">
      {lineNos}
    </pre>
  );
}

function CodeBlock(props) {
  const highlightedHTML = hljs.highlightAuto(props.textBlob).value;
  
  return (
    <pre>
      <code dangerouslySetInnerHTML={{ __html: highlightedHTML }} />
    </pre>
  );
}

function TextDisplayGrid(props) {
  return (
    <div className="flex">
      <div className="overflow-x-auto"><CodeBlock textBlob={props.textBlob} /></div>
      <div className="pr-8 order-first"><LineNos textBlob={props.textBlob} /></div>
    </div>
  );
}

function PageSubstance({ data }) {
  switch (data.resultType) {
    case 'treeContents':
      return (
        <div>
          <MidSection>
            <BreadCrumbTrail pathArray={data.resultPath} />
          </MidSection>
          <MainContentContainer>
            <DirectoryListing resultContent={data.resultContent} resultPath={data.resultPath} />
          </MainContentContainer>
        </div>
      );
      
      break;
    case 'textBlob':
      return (
        <div>
          <MidSection>
            <BreadCrumbTrail pathArray={data.resultPath} />
          </MidSection>
          <MainContentContainer>
            <TextDisplayGrid textBlob={data.resultContent} />
          </MainContentContainer>
        </div>
      );

      break;
    case 'binaryBlob':
        return (
          <div>
            <MidSection>
              <BreadCrumbTrail pathArray={data.resultPath} />
            </MidSection>
            <p>Binary content cannot be displayed.</p>
          </div>
        );
  
        break;
    case 'error':
      return (
        <div>
          <h1>Error:</h1>
          {data.resultContent}
        </div>
      );

      break;
  }
}

function Main({ data }) {
  return (
    <div>
      <header className="h-48 bg-slate-100 border-b border-slate-400"></header>
      <main>
        <div className="container mx-auto">
          <PageSubstance data={data} />
        </div>
      </main>
      <footer className="h-48"></footer>
    </div>
  );
}

export { getServerSideProps };
export default Main;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186